package com.zmn.plat.business.impl.plat.contract.model;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.common.dto.dept.DeptDRO;
import com.zmn.mcc.dubbo.interfaces.dept.DeptListRemoteService;
import com.zmn.plat.business.interfaces.plat.contract.model.PlatContractModelBService;
import com.zmn.plat.common.dictionary.PlatContractModelEnum;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.entity.plat.contract.model.PlatContractModel;
import com.zmn.plat.model.entity.plat.contract.model.log.PlatContractModelLog;
import com.zmn.plat.services.interfaces.plat.contract.model.PlatContractModelService;
import com.zmn.plat.services.interfaces.plat.contract.model.log.PlatContractModelLogService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class PlatContractModelBServiceImpl implements PlatContractModelBService {

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private DeptListRemoteService deptListRemoteService;

    @Resource
    protected PlatContractModelService platContractModelService;

    @Resource
    protected PlatContractModelLogService platContractModelLogService;

    @Override
    public Integer getPlatContractModel(Integer plat) {
        if (Objects.isNull(plat)) {
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        Integer result = platContractModelService.findByPlat(plat);
        if (NumberUtil.isNullOrZero(result)) {
            log.error("result is null.errorCode:{}", PlatErrorCodeDict.ERROR_CODE_PRODUCT_PLAT_CONTRACT_MODEL_NOT_EXIT);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PRODUCT_PLAT_CONTRACT_MODEL_NOT_EXIT);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @SuppressWarnings("Duplicates")
    public void modifyPlatContractModel(List<PlatContractModel> platContractModels, LoginStaff loginStaff) {
        Date now = DateUtil.getNow();
        String operator = loginStaff.getRealName();
        Integer deptId = loginStaff.getDeptId();
        StringBuffer stringBuffer = new StringBuffer();

        platContractModelService.deleteAllData();
        platContractModels.forEach(e -> {
            e.setCreater(operator);
            e.setCreateTime(now);
            e.setUpdater(operator);
            e.setUpdateTime(now);
            stringBuffer.append(ProductDict.getShowPlatName(e.getPlat())).append(":").append(PlatContractModelEnum.of(e.getContractModel()).getName()).append("，");
        });

        if (CollectionUtil.isNotNullOrEmpty(platContractModels)) {
            platContractModelService.insertBatch(platContractModels);
        }

        if (stringBuffer.length() > 0) {
            PlatContractModelLog platContractModelLog = new PlatContractModelLog();
            platContractModelLog.setContent(stringBuffer.deleteCharAt(stringBuffer.length() - 1).toString());
            platContractModelLog.setCreater(operator);
            platContractModelLog.setCreateTime(now);
            platContractModelLog.setUpdater(operator);
            platContractModelLog.setUpdateTime(now);

            ResponseDTO<DeptDRO> data = deptListRemoteService.getDept(deptId);
            DeptDRO dept = new DeptDRO();
            if (data.isSuccess()) {
                dept = data.getData();
            }
            platContractModelLog.setDeptId(deptId);
            platContractModelLog.setDeptName(dept.getDeptName());
            platContractModelLogService.insert(platContractModelLog);
        }
    }
}
